package com.yb.project.algorithm.practice.test;

import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        //System.out.println(Long.valueOf("0101",2));
        System.out.println(binaryStr(new String[]{"0111111111111111111111111111111111111111111111111111111111111111",
                "0011111111111111111111111111111111111111111111111111111111111111"},64));
        //System.out.println(Long.toBinaryString(Long.MAX_VALUE));
    }

    public static String binaryStr(String[] nums,int n){
        int times = n/63;
        int k = n%63;
        if(k>0){
            times++;
        }
        for(int i=0;i<times;i++){
            int start = i*63;
            int end = start;
            if(k>0 && i==times-1){
                end = start+k;
            }else{
                end = start+63;
            }
            String[] copy = new String[nums.length];
            for(int j=0;j<copy.length;j++){
                copy[j]=nums[j].substring(start,end);
            }
            String res = binaryStr_sub(copy,63);
            if(res !=null){
                return res+nums[0].substring(end);
            }
        }
        return null;
    }

    // n<=63
    public static String binaryStr_sub(String[] nums,int n){
        Set<Long> set =new HashSet<>();
        for(int j=0;j<nums.length;j++){
            set.add(Long.valueOf(nums[j],2));
        }
        Long max = (long)Math.pow(2,n-1);
        for(long i=0;i<=max;i++){
            if(!set.contains(i)){
                String res = Long.toBinaryString(i);
                int len = res.length();
                StringBuilder sb = new StringBuilder();
                while(len<n){
                    sb.append(0);
                    len++;
                }
                return sb.append(res).toString();
            }
        }
        return null;
    }
}
